package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class BjnewsNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://www.bjnews.com.cn/feature/2012/11/22/235031.html
		// http://news.bjnews.com.cn/2011/0425/116470.shtml
		// http://www.bjnews.com.cn/ent/2012/11/22/235051.html
		boolean bRet=false;
		String[] regex={
				"http://.*[.]bjnews[.]com[.]cn/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*[.]html",
				"http://.*[.]bjnews[.]com[.]cn/[0-9]{4}/[0-9]{4}/[0-9]*[.]shtml" 
				
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";
		String url=urlMeta.getUrl();
		Long date=null;
		String source="新京报";
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title=doc.select("title").text();
		title = StringUtils.substringBefore(title, "_");
		if(StringUtils.isBlank(title)){
			title=doc.select("h1").text();
		}

		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("dl.ctdate,.date").text());

		if (date == null) {
			Elements select=doc.select("div.zuoz");
			if (select.size() > 0) {
				String html2=select.html();
				int pos=html2.indexOf("<script");
				if (pos != -1) {
					String tmpLeft=html2.substring(0, pos);
					String tmpRight=html2.substring(pos);
					pos=tmpLeft.indexOf("</span>");
					if (pos != -1) {
						tmpLeft=tmpLeft.substring(pos + "</span>".length());
					}
					int indexOf=tmpRight.indexOf("var createTime ='");
					if (indexOf != -1) {
						tmpRight=tmpRight.substring(indexOf
								+ "var createTime ='".length());
						indexOf=tmpRight.indexOf("'");
						if (indexOf != -1) {
							tmpRight=tmpRight.substring(0, indexOf);
						}
					}
					date=DateUtils.matchDate((tmpLeft + " " + tmpRight)	.replace("\n", ""));
				}
			}
		}
		/*
		 * 解析内容
		 */
		doc.select("strong:contains(本文首发自新京报微信，您可以在微信中搜索公众号“新京报”，或添加微信号：bjnews_xjb了解更多精彩内容。)").remove();

		content=HtmlCleaner.getContentHtml(url ,doc.select("div.content"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url ,doc.select("div.wzcot"));
		}
		if(StringUtils.isBlank(title)||date==null){
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		String author= JSoupUtils.matchAuthor(doc, "来源:");
		meta.setAuthor(author);
		return meta;
	}

	public static void main(String[] args) {

		BjnewsNewsAnalyse cnhubeiNewsAnalyse=new BjnewsNewsAnalyse();
		String url="http://www.bjnews.com.cn/news/2016/11/08/422586.html";
		url = UrlArgumentTop.FromatUrl(url);
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=cnhubeiNewsAnalyse.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	} 
	
	public boolean isNeedUpdate(){
		return false;
	}
}

